import JsDataStore  from './JsDataStore'
import { HashMap } from '@kit.ArkTS';

export class ObjectBuilder<Args extends Object[]> {
  builder: (...args: Args) => Object;
  constructor(builder: (...args: Args) => Object) {
    this.builder = builder;
  }
}


export class JsQtModule {
  moduleJsObjects: HashMap<string, Object> = new HashMap();

  constructor() {
    JsDataStore.addJsModule(this);
  }

  hasJsObjectType(type: string): boolean {
    if (this.moduleJsObjects == undefined)
      return false;
    return this.moduleJsObjects.hasKey(type);
  }

  createJsObjectImpl(type: string, ...args: any[]): Object | undefined {
    if (this.moduleJsObjects == undefined)
      return undefined;
    let c = this.moduleJsObjects.get(type);
    return (c as ObjectBuilder<Object[]>)?.builder(...args);
  }

  createJsObject(type: string, name: string, ...args: any[]): Object {
    let obj = this.createJsObjectImpl(type, ...args);
    return obj;
  }
}